/* ******************************************************************
����:buzzer.c 
����:������ģ��Դ�ļ�  
 ����:Ҧ��ƽ  
����:2010-01-04 
****************************************************************** */
/* ******************************************************************
   ע������  
****************************************************************** */ 
/* 
   һ��������ڲ��?��Ƭ��ƽ̨���Լ���ͬ��Ŀ����ֲ����ʱ���ļ��������κ��޸ￄ1?71�?1?77! 
 */
/* *************************************************************************************************
                   ͷ�ļ�������   
****************************************************************** */
#define  __BUZZER_C__
//#include "global.h"
#include "buzzer.h"
#include "machine_stat.h"
#undef   __BUZZER_C__

#if defined( PRJ_OPTION_BUZZER ) && ( PRJ_OPTION_BUZZER == ON )

uint8_t sgBuzzer_priority=0;

BUZZER_STAT buzzer_stat=BUZZER_ON;

enum{
   BUZZER_POL_KEY = 1,
   BUZZER_POL_ERR = 2,
   BUZZER_POL_BATHTUBE = 3,
};

pwmGen_t Pwm1, Pwm2;
void PWM_Config(pwmGen_t *pwm, uint8_t on_time, uint8_t off_time, uint16_t pwm_count);


void Buzzerinit(void)
{
  PWM_Config(&Pwm1, 150, 0, 1);
  PWM_Config(&Pwm2, 150, 0, 1);
}

void PWM_Loop(pwmGen_t *pwm){
    pwm->Timer++;
    if(pwm->OffTime == 0 && pwm->OnTime == 0)
       pwm->Output = 0;
    else if(pwm->OffTime == 0xff && pwm->OnTime == 0xff)
        pwm->Output = 1;
    else{
        if(pwm->PwmCount != 0){
            if(pwm->Timer >= pwm->OnTime + pwm->OffTime){
                pwm->Timer = 0;
                if(pwm->PwmCount != 0xFFFF)
                    pwm->PwmCount--;
            }else if(pwm->Timer <= pwm->OnTime)
                pwm->Output = 1;
            else
                pwm->Output = 0;
        }else
            pwm->Output = 0;
    }
}
/** @brief 配置PWM参数，配置后立即生效
 *  @param pwm 指向PWM结构体的指针
 *  @param on_time 占空比高电平时间
 *  @param off_time 占空比低电平时间
 *  @param pwm_count 重复次数
 */
void PWM_Config(pwmGen_t *pwm, uint8_t on_time, uint8_t off_time, uint16_t pwm_count){
   pwm->OffTime = off_time;
   pwm->OnTime = on_time;
   pwm->PwmCount = pwm_count; 
pwm->Timer = 0;    
}

void BuzzerTask( void ){
   PWM_Loop(&Pwm1);
   PWM_Loop(&Pwm2);
   if(Pwm1.Output == 1 && Pwm2.Output == 1)
       TURN_ON_BUZZER();
   else
       TURN_OFF_BUZZER();
   if(Pwm1.PwmCount == 0 && Pwm2.PwmCount == 0) {
    PWM_CmdEX(PWM3_Type,DISABLE);
    sgBuzzer_priority=0;
   }
              
}

void Buzzer_key(void)
{
  if(buzzer_stat == BUZZER_OFF) return;
  if(sgBuzzer_priority<1)
  {
    PWM_CmdEX(PWM3_Type,ENABLE);
      
    PWM_Config(&Pwm1, 6, 0, 1);
    PWM_Config(&Pwm2, 6, 0, 1);
    sgBuzzer.u8BuzzerOffTime=0;
    sgBuzzer.u8BuzzerOnTime=6;
    sgBuzzer.u8BuzzerSoundCount=1;
    sgBuzzer_priority=1;
  }
  
}
void Buzzer_open(void)
{
  buzzer_stat=BUZZER_ON; 
  if(sgBuzzer_priority<3)
  {
    PWM_CmdEX(PWM3_Type,ENABLE);

    PWM_Config(&Pwm1, 6, 50, 3);
    PWM_Config(&Pwm2, 6, 50, 3);      
    sgBuzzer_priority=3;
  }
  
}
void Buzzer_close(void)
{
  buzzer_stat=BUZZER_OFF;
  if(sgBuzzer_priority<3)
  {
    PWM_CmdEX(PWM3_Type,ENABLE);

    PWM_Config(&Pwm1, 6, 50, 2);
    PWM_Config(&Pwm2, 6, 50, 2);       
    sgBuzzer_priority=3;
  }
}
void Buzzer_err(void)
{
  if(sgBuzzer_priority<2)
  {
    PWM_CmdEX(PWM3_Type,ENABLE);
    PWM_Config(&Pwm1, 100, 100, 30);
    PWM_Config(&Pwm2, 100, 100, 30);
    sgBuzzer_priority=2;
  }
}
/** @brief 浴缸蜂鸣器报警, 优先级最高 */  
void Buzzer_Bathtube(void)
{
  if(sgBuzzer_priority<2)
  {
    PWM_CmdEX(PWM3_Type,ENABLE);
    PWM_Config(&Pwm1, 6, 2, 750);
    PWM_Config(&Pwm2, 24, 24, 125);      
    sgBuzzer_priority=2;
  }
}

void Buzzer_clear(void)
{
  PWM_Config(&Pwm1, 0, 0, 30);
  PWM_Config(&Pwm2, 0, 0, 30);
  sgBuzzer_priority=0;
}

void Buzzer_clear_err_in_poweroff(void)
{
	if(sgBuzzer_priority==2)
	{
		Buzzer_clear();
	}
}
/* ************************************************************************************************ */
#endif /*  PROJECT_OPTION_BUZZER  */
/* ******************************************************************
                                       END OF FILE
****************************************************************** */


